package xhwl.insurance.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.trello.rxlifecycle.components.RxActivity;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.AllCommentListAdapter;
import xhwl.insurance.http.api.CommentReplyListApi;
import xhwl.insurance.http.api.ForumDetailListApi;
import xhwl.insurance.http.api.ReplyReadMsgApi;
import xhwl.insurance.http.bean.resp.ForumAllCommentResp;
import xhwl.insurance.http.bean.resp.ForumProductResp;
import xhwl.insurance.http.bean.resp2.HttpResp;
import xhwl.insurance.util.SpUtil;
import xhwl.pulltorefresh.PullToRefreshRecyclerView;
import xhwl.pulltorefresh.callback.PullToRefreshListener;
import xhwl.retrofitrx.BaseApi;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by Administrator on 2017/10/12.
 * 回复我的
 */

public class ReplyListActivity extends BaseActivity implements PullToRefreshListener{

    private PullToRefreshRecyclerView recyclerView;
    String mPage = "1";
    List<ForumAllCommentResp> CommentList = new ArrayList<>();
    private AllCommentListAdapter mAllCommentListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reply_mine);
       getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);


    }

    @Override
    public void initViews() {
        initTitleViews();
        recyclerView = (PullToRefreshRecyclerView) findViewById(R.id.recyclerView);
        initRecylerView();
        mAllCommentListAdapter = new AllCommentListAdapter(ReplyListActivity.this,CommentList);
        recyclerView.setAdapter(mAllCommentListAdapter);
    }
    private void initRecylerView() {
        //添加HeaderView
//        View headView = View.inflate(ReplyListActivity.this, R.layout.layout_find_head, null);
//        recyclerView.addHeaderView(headView);
        //设置EmptyView
        View emptyView = View.inflate(ReplyListActivity.this, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        recyclerView.setEmptyView(emptyView);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(ReplyListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);


            }
        });
        //设置是否开启上拉加载
        recyclerView.setLoadingMoreEnabled(true);
        //设置是否开启下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        //设置是否显示上次刷新的时间
        recyclerView.displayLastRefreshTime(false);
    }
    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("回复我的");
    }

    @Override
    public void initListener() {
        //设置刷新回调
        recyclerView.setPullToRefreshListener(this);
        mAllCommentListAdapter.setOnItemClickLitener(new AllCommentListAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(int position) {
                getForumDetailData(position);
            }

        });

    }

    private void getForumDetailData(final int position) {
        final String post_id=CommentList.get(position).getPOST_ID();
        BaseApi api = new ForumDetailListApi(post_id);
        HttpManager.getInstance().dealHttp(ReplyListActivity.this, api, new OnNextListener<HttpResp<ArrayList<ForumProductResp>>>() {

            private ForumProductResp obj;

            @Override
            public void onNext(HttpResp<ArrayList<ForumProductResp>> arrayListHttpResp) {
                if (arrayListHttpResp != null) {
                    if (arrayListHttpResp.isOk()) {
//                        Toast.makeText(ReplyListActivity.this, "post_id"+CommentList.get(position).getPOST_ID()+"=="+CommentList.get(position).getPOST_TITLE(), Toast.LENGTH_SHORT).show();
                        if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                            ArrayList<ForumProductResp> al = new ArrayList<>();
                            al.addAll(arrayListHttpResp.data);
                            for (int i = 0; i < al.size(); i++) {
                                obj = (ForumProductResp) al.get(i);
                            }
                            Intent intent = new Intent();
                            intent.putExtra(ForumDetailNewActivity.EXTRA_DATA, obj);
                            intent.setClass(ReplyListActivity.this, ForumDetailNewActivity.class);
                           startActivity(intent);
                        }
                    }

                }
            }

        });
    }


    @Override
    public void loadData() {
        //加载所有评论数据列表
        getAllCommentData("1");
        //调取已读接口
        getReadMsg();
    }

    private void getReadMsg() {
        final String member_id = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID, "");
        BaseApi api=new ReplyReadMsgApi("3",member_id);
        HttpManager.getInstance().dealHttp(ReplyListActivity.this, api, new OnNextListener<HttpResp>(){

            @Override
            public void onNext(HttpResp httpResp) {
                if (httpResp.isOk()){
                    SpUtil.putBoolean(SpUtil.REPLY_MAG_FLAG, false);
                }

            }
        });
    }

    private void getAllCommentData(final String page) {
        final String member_id = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID, "");
        BaseApi api=new CommentReplyListApi(page,member_id);
        HttpManager.getInstance().dealHttp(ReplyListActivity.this, api, new OnNextListener<HttpResp<ArrayList<ForumAllCommentResp>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<ForumAllCommentResp>> arrayListHttpResp) {
                if (page == "1") {
                    if (arrayListHttpResp != null) {
                        recyclerView.setRefreshComplete();
                        if (arrayListHttpResp.isOk()) {
                            mPage = "1";
                            CommentList.clear();
                            if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                                CommentList.addAll(arrayListHttpResp.data);
                            }
                            mAllCommentListAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.showToast(ReplyListActivity.this, arrayListHttpResp.message);
                        }
                    } else {
                        recyclerView.setRefreshFail();
                    }
                } else {
                    if (arrayListHttpResp != null) {
                        recyclerView.setLoadMoreComplete();
                        if (arrayListHttpResp.isOk()) {
                            if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                                mPage = page;
                                CommentList.addAll(arrayListHttpResp.data);
                                mAllCommentListAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtil.showToast(ReplyListActivity.this, getString(R.string.no_more_data));
                            }
                        } else {
                            ToastUtil.showToast(ReplyListActivity.this, arrayListHttpResp.message);
                        }
                    } else {
                        recyclerView.setLoadMoreFail();
                    }
                }
            }
            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                if (page == "1") {
                    recyclerView.setRefreshFail();
                } else {
                    recyclerView.setLoadMoreFail();
                }
            }
        });

    }

    @Override
    public void processOper() {
        getAllCommentData("1");
        Log.i("1019","===========================1");

    }

    @Override
    public void onResume() {
        getAllCommentData("1");
        Log.i("1019","===========================");
        super.onResume();

    }

    @Override
    public void onRefresh() {
        getAllCommentData("1");

    }

    @Override
    public void onLoadMore() {
        getAllCommentData(mPage + 1);
    }


}
